package com.MyDemo.demo.service;


import com.MyDemo.demo.domain.LoginUser;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class MyUserDetailService implements UserDetailsService {
    Logger logger = LoggerFactory.getLogger(MyUserDetailService.class);
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryChainWrapper<LoginUser> userLambdaQueryWrapper = new LambdaQueryChainWrapper<>(LoginUser.class);
        LoginUser selectedLoginUser = userLambdaQueryWrapper.eq(LoginUser::getUsername, username).one();

        UserDetails userDetails = User.withDefaultPasswordEncoder()
                .username(username)
                .password(selectedLoginUser.getPassword())
                .roles("USER")
                .build();
        logger.error(selectedLoginUser.toString());
        return userDetails;
    }
}
